package com.wye.controller;

import com.github.pagehelper.PageInfo;
import com.wye.goods.pojo.Spec;
import com.wye.service.SpecService;
import entity.Result;
import entity.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/spec")
@CrossOrigin
public class SpecController {

    @Autowired
    private SpecService specService;




    @GetMapping(value = "/category/{id}")
    public Result findByCategory(@PathVariable(value = "id") Integer categoryId){
        List<Spec> specs = specService.findByCategoryId(categoryId);
        return new Result(true, StatusCode.OK, "查询成功", specs);
    }

    /**
     * 查询所有品牌
     */
    @GetMapping("/findAll")
    public Result<List<Spec>> findAll(){
        List<Spec> specs = specService.findAll();
        return new Result<List<Spec>>(true, StatusCode.OK, "查询品牌集合", specs);
    }

    @GetMapping("/findSpecById/{id}")
    public Result findSpecById(@PathVariable(value = "id")Integer id){
        Spec spec = specService.findById(id);
        if (spec == null){
            return new Result(false, StatusCode.ERROR, "请求失败");
        }
        return new Result(true, StatusCode.OK, "请求成功", spec);
    }

    @PostMapping(value = "/add")
    public Result add(@RequestBody Spec Spec){
        int resultInt = specService.insert(Spec);
        if (resultInt != 0){
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result updatespec(@RequestBody Spec spec){
            specService.update(spec);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") Integer id){
            specService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

//    @PostMapping(value = "/search")
//    public Result<List<Spec>> search(Spec Spec){
//        //调用service实现查询
//        if (Spec != null){
//            List<Spec> Specs = SpecService.findList(Spec);
//            return new Result<List<Spec>>(true, StatusCode.OK, "查询成功", specs);
//        }
//        return new Result<List<Spec>>(false, StatusCode.ERROR, "请输入有效参数");
//    }

//    @RequestMapping(value = "/search/{page}/{size}")
//    public Result findPage(@PathVariable(value = "page")Integer page, //当前页
//                           @PathVariable(value = "size")Integer size,
//                           Spec spec){//每页显示的条数
//        if (spec == null){
//            PageInfo<Spec> pageInfo = specService.findPage(page, size);
//            return new Result(true, StatusCode.OK, "分页查询成功", pageInfo);
//        }
//        PageInfo<Spec> pageInfo = specService.findPage(page, size, spec);
//        return new Result(true, StatusCode.OK, "分页搜索查询成功", pageInfo);
//
//    }


}
